<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="通知标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入通知标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工名称" prop="title">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:gwnotice:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="gwnoticeList">
      <el-table-column :formatter="defaultTableColumnDate" label="ID" align="center" prop="id" />
      <el-table-column :formatter="defaultTableColumnDate" label="通知标题" align="center" prop="title" />
      <el-table-column :formatter="defaultTableColumnDate" label="已读人数" align="center" prop="readNum">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleClick(scope.row, '1')">{{ scope.row.readNum }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="未读人数" align="center" prop="unReadNum">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleClick(scope.row, '0')">{{ scope.row.unReadNum }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="更新人" align="center" prop="updateBy" />
      <el-table-column :formatter="defaultTableColumnDate" label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:gwnotice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:gwnotice:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleForm(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通知公告对话框 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <div v-for="(item, index) in gwNoticeFileList" :key="index">
          <el-form-item label="内容描述">
            <el-input v-model="item.note" maxlength="500" type="textarea" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="上传图片">
            <image-upload v-model="item.url"/>
          </el-form-item>
        </div>
        <el-divider></el-divider>
        <el-form-item>
          <el-button type="primary" @click="gwNoticeFileList.push({})">添加内容描述</el-button>
          <el-button v-if="gwNoticeFileList.length > 1" @click="gwNoticeFileList.pop()">删除内容描述</el-button>
        </el-form-item>
        <el-form-item label="发送员工" required>
          <el-checkbox v-model="statusFlag">仅显示在职员工</el-checkbox>
          <div style="height: 400px;box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);overflow-y: auto;padding: 20px;">
            <el-row :gutter="10" class="mb8">
              <el-col :span="12">
                <el-input v-model="searchVal" placeholder="请输入"></el-input>
              </el-col>
              <el-col :span="12">
                <el-button type="primary" @click="search">搜索</el-button>
              </el-col>
            </el-row>
            <el-checkbox-group v-model="checkedList">
              <div v-for="item in treeList" :key="item.id">
                <el-checkbox :label="item.id">{{ item.staffName }}</el-checkbox>
              </div>
            </el-checkbox-group>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog destroy-on-close title="查看通知" :visible.sync="formopen" width="1000px" append-to-body>
      <h2 style="text-align: center;">{{ form.title }}</h2>
      <p style="text-align: center;">{{ form.createTime }}</p>
      <div style="padding: 20px;" v-for="item in gwNoticeFileList" :key="item.id">
        <p>{{ item.note }}</p>
        <img v-if="item.url" style="width: 60%;height: auto;text-align: center;" :src="item.url" alt="">
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog destroy-on-close title="查看人员数据" :visible.sync="readopen" width="1000px" append-to-body>
      <el-tabs v-model="statusName" type="card" @tab-click="handleClick()">
        <el-tab-pane label="已读" name="1">
        </el-tab-pane>
        <el-tab-pane label="未读" name="0">
        </el-tab-pane>
      </el-tabs>
      <div class="a-list" v-if="readList.length">
        <div class="a-list-item" v-for="(item, index) in readList" :key="index">
          <div>{{ item.staffName }}</div>
          <div>{{ item.phone }}</div>
        </div>
      </div>
      <el-empty v-if="readList.length == 0" description="暂无数据"></el-empty>
      
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="staffopen=true">添加发送对象</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog destroy-on-close title="添加发送对象" :visible.sync="staffopen" width="1000px" append-to-body>
      <el-form ref="form" label-width="80px">
        <el-form-item label="发送员工" required>
          <el-checkbox v-model="statusFlag">仅显示在职员工</el-checkbox>
          <div style="height: 400px;box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);overflow-y: auto;padding: 20px;">
            <el-row :gutter="10" class="mb8">
              <el-col :span="12">
                <el-input v-model="searchVal" placeholder="请输入"></el-input>
              </el-col>
              <el-col :span="12">
                <el-button type="primary" @click="search">搜索</el-button>
              </el-col>
            </el-row>
            <el-checkbox-group v-model="checkedList">
              <div v-for="item in treeList" :key="item.id">
                <el-checkbox :label="item.id">{{ item.staffName }}</el-checkbox>
              </div>
            </el-checkbox-group>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addstaff">添加发送对象</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGwnotice, getGwnotice, delGwnotice, addGwnotice, updateGwnotice, getStaffReadList, addStaffList } from "@/api/system/gwnotice";
import { getListStaff } from "@/api/system/staff";

export default {
  name: "Gwnotice",
  data() {
    return {
      readList: [],
      statusName: '',
      readopen: false,
      formopen: false,
      staffopen: false,
      searchVal: '',
      checkedList: [],
      statusFlag: true,
      treeList: [],
      oldTreeList: [],
      defaultProps: {
        label: 'staffName'
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通知公告表格数据
      gwnoticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        title: null,
        notes: null,
        url: null,
      },
      // 表单参数
      form: {},
      gwNoticeFileList: [],
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        notes: [
          { required: true, message: "内容描述不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    statusFlag() {
      this.getTree();
    },
  },
  created() {
    this.getList();
    this.getTree();
  },
  methods: {
    addstaff() {
      if(!this.checkedList.length) {
        this.$modal.msg(`请选择员工!`);
        return
      }
      const gwNoticeStaffList = this.checkedList.map(v => {
        return { staffId: v, noticeId: this.readInfo.id }
      })
      addStaffList({id: this.readInfo.id, gwNoticeStaffList}).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.cancel()
        this.getList();
      })
    },
    handleClick(row, status) {
      if(row) {
        this.readInfo = row
      }
      if(status) {
        this.statusName = status
      }
      this.getReadStatus();
    },
    async getReadStatus() {
      this.reset();
      const res = await getStaffReadList({ noticeId: this.readInfo.id, status: this.statusName })
      this.readList = res.data
      this.readopen = true;
    },
    search() {
      if(this.searchVal) {
        this.treeList = this.oldTreeList.filter(v => v.staffName.includes(this.searchVal))
      } else {
        this.treeList = this.oldTreeList
      }
    },
    getTree() {
      getListStaff({status: this.statusFlag?'在职':'离职'}).then(response => {
        this.treeList = response.data
        this.oldTreeList = response.data
        this.searchVal = ''
      });
    },
    /** 查询通知公告列表 */
    getList() {
      this.loading = true;
      listGwnotice(this.queryParams).then(response => {
        this.gwnoticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.readopen = false
      this.formopen = false
      this.staffopen = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        title: null,
        notes: null,
        url: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.gwNoticeFileList=[]
      this.checkedList = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.gwNoticeFileList=[{}]
      this.open = true;
      this.title = "添加通知公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGwnotice(id).then(response => {
        this.form = response.data;
        this.gwNoticeFileList = response.data.gwNoticeFileList;
        this.checkedList = response.data.gwNoticeStaffList.map(v => Number(v.staffId));
        this.open = true;
        this.title = "修改通知公告";
      });
    },
    handleForm(row) {
      this.reset();
      const id = row.id || this.ids
      getGwnotice(id).then(response => {
        this.form = response.data;
        this.gwNoticeFileList = response.data.gwNoticeFileList;
        this.formopen = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(!this.checkedList.length) {
            this.$modal.msg(`请选择员工!`);
            return
          }
          let flag = false
          this.gwNoticeFileList.forEach(v => {
            if(!v.note) {
              flag = true
            }
          })
          if(flag) {
            this.$modal.msg('请完善内容描述')
            return
          }
          const gwNoticeStaffList = this.checkedList.map(v => {
            return { staffId: v }
          })
          if (this.form.id != null) {
            updateGwnotice({...this.form, gwNoticeFileList: this.gwNoticeFileList, gwNoticeStaffList }).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGwnotice({...this.form, gwNoticeFileList: this.gwNoticeFileList, gwNoticeStaffList }).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除通知公告编号为"' + ids + '"的数据项？').then(function() {
        return delGwnotice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/gwnotice/export', {
        ...this.queryParams
      }, `gwnotice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
